<?php
include_once '../include/inc_session.php';
	
	$sEdition 	= $_GET['edition'];
	$sDateDebut = $_GET['dd'];
	$sDateFin 	= $_GET['df'];
	
	$oElementDico = new dataObject_elementdico();
	$oEditions	  = new dataObject_editions();
	
	$sNomModule = $oBloc->_MENU_NOM;
	$sNomListe;
	$bEnvoiMail = false;

	//on envoi le mail si click sur le bouton envoi mail
	if($gbFormValid && isset($_POST['Envoyer']))
	{
		//$sMsgWait = "Veuillez patienter pendant l'envoi du mail... ";
		include_once 'succes_envoi_mail.php';
	}
    if(isset($_POST['Retour'])) header('location:index.php');
    
	$aEnteteTab = array(
	array("num_colonne"=>1, "nom_colonne"=>"Contrat"),
	array("num_colonne"=>2, "nom_colonne"=>"Date effet"),
	array("num_colonne"=>3, "nom_colonne"=>"Offre"),
	array("num_colonne"=>4, "nom_colonne"=>"Produit"),
	array("num_colonne"=>5, "nom_colonne"=>"Formule")
	);

	
	switch ($sEdition)
	{
		case '134':
				header("Location:echeances.php?edition=$sEdition&dd=$sDateDebut&df=$sDateFin");
				break;
		case '135':
				$sNomListe = $oElementDico->elementdico_getNomFromCode(AG,dataObject_dictionnaire::LISTES_EDITION);
				$aEnteteTab = array_merge($aEnteteTab,array(array("num_colonne"=>6, "nom_colonne"=>"Acte de gestion")
				));
				$sTabActe = "
				<tr id=\"niv1\">
					<td class='col3'  style=' width:30%; height:25px; font-weight:bold;'>&nbsp;</td>
					<td class='col3'  style=' width:40%; height:25px; font-weight:bold;' align='center'>Acte de gestion</td>
					<td class='col3'  style=' width:30%; height:25px; font-weight:bold;' align='center'>Nombre d'actes</td>
				</tr>";
				$aPdfWidthRecord_135 = array("Contrat" => 16,
										   "Date effet" => 12,
						"Offre" => 14,
						"Produit" => 14,
						"Formule" => 20,
						"Acte de gestion" => 24
						);
				$aActe = $aActeUnite  = array();
				break;
		case '136':
				header("Location:echeances.php?edition=$sEdition&dd=$sDateDebut&df=$sDateFin");
				break;
	
		case '137':
				$sNomListe = $oElementDico->elementdico_getNomFromCode('SID',dataObject_dictionnaire::LISTES_EDITION);
				$aEnteteTab = array_merge($aEnteteTab,array(array("num_colonne"=>6, "nom_colonne"=>"Type"),
				array("num_colonne"=>7, "nom_colonne"=>"Date <br/>survenance&nbsp;déclaration"),
				array("num_colonne"=>9, "nom_colonne"=>"Etat"),
				array("num_colonne"=>10, "nom_colonne"=>"D&eacute;cision"),
				array("num_colonne"=>11, "nom_colonne"=>"Motif refus")
				));
				$sTabSinistreDec = "
				<tr id=\"niv1\">
					<td class='col3'  style=' width:34%; height:25px;'>&nbsp;</td>
					<td class='col3' align='center'  style=' width:33%; height:25px;  font-weight:bold;'>Type de sinistre</td>
					<td class='col3' align='center'  style=' width:33%; height:25px; font-weight:bold;'>Nombre de sinistres</td>
				</tr>";
				$aPdfWidthRecord_137 = array("Contrat" => 12,
										   "Date effet" => 7,
						"Offre" => 10,
						"Produit" => 10,
						"Formule" => 16,
						"Type" => 7,
						"Date <br/>survenance&nbsp;déclaration" => 14,
						"Etat" => 7,
						"D&eacute;cision" => 7,
						"Motif refus" => 10
						);
				$aSinistreDec = $aSinistreDecUnite  = array();
				break;
	
		case '138':
				$sNomListe = $oElementDico->elementdico_getNomFromCode(SIR,dataObject_dictionnaire::LISTES_EDITION);
				$aEnteteTab = array_merge($aEnteteTab,array(array("num_colonne"=>6, "nom_colonne"=>"Type"),
				array("num_colonne"=>7, "nom_colonne"=>"Date <br/>survenance&nbsp;déclaration"),
				/*array("num_colonne"=>8, "nom_colonne"=>"Date déclaration"),*/
				array("num_colonne"=>9, "nom_colonne"=>"Type prestation"),
				array("num_colonne"=>10, "nom_colonne"=>"Montant"),
				array("num_colonne"=>11, "nom_colonne"=>"Date r&eacute;glement")
				));
				$sTabSinistreReg = "
				<tr id=\"niv1\">
					<td class='col1' style=' width:22%;'>&nbsp;</td>
					<td class='col3' align='center'  style=' width:20%; font-weight:bold;'>Type de sinistre</td>
					<td class='col1' align='center'  style=' width:18%; font-weight:bold;'>Nombre sinistres</td>
					<td class='col3' align='center'  style=' width:20%; font-weight:bold;'>Type de prestation</td>
					<td class='col1' nowrap='nowrap' align='center'  style=' width:20%; font-weight:bold;'>Montant prestations</td>
				</tr>";
				$aPdfWidthRecord_138 = array("Contrat" => 14,
										   "Date effet" => 8,
						"Offre" => 10,
						"Produit" => 10,
						"Formule" => 12,
						"Type" => 7,
						"Date <br/>survenance&nbsp;déclaration" => 14,
						"Type prestation" => 8,
						"Montant" => 7,
						"Date r&eacute;glement" => 10
						);
				$aSinistreReg = $aSinistreRegUnite  = array();
				$aPrestation = $aPrestationUnite  = array();
				break;
	
		case '139':
	
				$sNomListe = $oElementDico->elementdico_getNomFromCode(SO,dataObject_dictionnaire::LISTES_EDITION);
				$aEnteteTab = array_merge($aEnteteTab,array(
					array("num_colonne"=>6, "nom_colonne"=>"Cotisation"),
					array("num_colonne"=>7, "nom_colonne"=>"Droit entr&eacute;e"),
					array("num_colonne"=>8, "nom_colonne"=>"Fr&eacute;quence")));
				$sTabSous = "
					<tr id='niv1'>
						<td class='col3' style='text-align:center;  width:24%; font-weight:bold;'>&nbsp;</td>
						<td class='col3' align='center' style='text-align:center;  width:26%; font-weight:bold;'>Nombre Contrats</td>
						<td class='col3' align='center' style='text-align:center;  width:30%; font-weight:bold;'>Montant Cotisations</td>
						<td class='col3' align='center'style='text-align:center;  width:20%; font-weight:bold;'>Montant Droits entr&eacute;e</td>
					</tr>";
				$aPdfWidthRecord_139 = array("Contrat" => 14,
										   "Date effet" => 10,
						"Offre" => 12,
						"Produit" => 14,
						"Formule" => 20,
						"Cotisation" => 9,
						"Droit entr&eacute;e" => 9,
						"Fr&eacute;quence" => 12
						);
				$fTotalCotisation = $iNbContrat = $fTotalDE = 0;
				break;
	
	}
	$sSepareTable = '</table><div id="br2"></div>
				 <table class="tab_list" border="1" style=" border:1px solid black; border-collapse:collapse;">';
	$sSepareTableVide = '<div id="br2"></div>
				 <table class="tab_list" width=100% border="1" style=" border:1px solid black; border-collapse:collapse;">'; 

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title>Editions</title>
			<link rel="stylesheet" type="text/css" href="../css/main_editions.css" />
            <link rel="stylesheet" type="text/css" href="../css/list_editions.css" />
			<!--[if IE 7]>
            	<link rel="stylesheet" type="text/css" href="../css/ie7.css" />
            	<link rel="stylesheet" type="text/css" href="form_ie7.css" />
            <![endif]-->
            <script> 
			function printEdition(urlImpression)
			    {
			    	options = "Width=850,Height=670" ;
			    	window.open(urlImpression,"editions_unite", options) ;
			    }
			</script>
	</head>
	<body>
		<div id="sf_menu1">
			<h3><?php echo $sNomModule ?></h3>
			<div id="information_erreur">
			<?php 
				if(isset($sMsgWait)) echo colorer($sMsgWait, "false");
				$aMessages = $oForm->getValidMessage();
				if(!empty($aMessages))
				{
			?>
				<ul id="message">
					<li><?php echo $aMessages[0]['message'] ?></li>
				</ul>
			<?php } ?>
			</div>
		</div><!-- fin sf_menu1 -->
		
		<div class="cleared"></div>
		
		<div class="corps1">
			
		<!--/***************************************/-->
			<div class="imgleft1"></div>
			
				
			<div class="imgmiddle1">
			
				<div class="btn">
		        	<span class="btnleft"></span>
		            <span class="btnmiddle">
		            	<h5>Demande d'&eacute;dition</h5>
		            </span><span class="btnright"></span>
		        </div>
				<div class="btn1">
		        	<span class="btnleft"></span>
		            <span class="btnmiddle">
		            	<h5>D&eacute;tail &eacute;dition</h5>
		            </span><span class="btnright"></span>
		        </div>
		        <PDF>
				<table align="center">
					<tr>
						<td align="center"><font size="2"><?php echo $sNomListe.'	du	'.formaterDate($sDateDebut,"-","/",false).'	au	'.formaterDate($sDateFin,"-","/",false)?></font></td>
					</tr>
				</table>
				<div id="br2"></div>
<?php
	if (!$bEnvoiMail)
	{

?>
				<div class="content_table bright">
			<?php
			$idUnite = $oEditions->idUnite($_SESSION[iduser]);
			$asDetailUnite = $oEditions->infosUnite($idUnite);
		
			//on ne recupere que les agents qui ont enregistre un sinitre dans cet edition
			if($sEdition == '137')
			{
				$aListesAgents = $oEditions->getAgentWithSinDeclareUnite($idUnite, $sDateDebut, $sDateFin);
			}
			else 
			{
				$aListesAgents = $oEditions->agentsUnite($idUnite);
				//on considere aussi l'agent 'NULL' pour les traitements différés
				$aListesAgents[] = array('Identifiant_agent' => '0', 
									 'Code_agent'=> '', 
									 'Nom_agent'=> '' ,
									 'Identifiant_unite'=> $idUnite);
			}
			
			$iTotalGeneral = $iMontPrest = 0;
			$fCotisation = $fDroit = $fPrestation = $fTotalCotisation = $fTotalDroit = $fTotalPrestation = 0;
			$iMontPrestUnite = $fCotisationUnite = $fDroitUnite = 0;
			?>
			<table style="width:400px; text-align: left">
					<tr>
						<td style="width:20%; text-align: left;">R&eacute;gion : </td>
						<td style="width:25%; text-align: left;"><?php echo $asDetailUnite['Code_region']?></td>
						<td style="width:55%; text-align: left;"><?php echo $asDetailUnite['Nom_region']?></td>
					</tr>
					<tr>
						<td style="width:20%; text-align: left;">Unit&eacute; : </td>
						<td style="width:25%; text-align: left;"><?php echo $asDetailUnite['Code_unite']?></td>
						<td style="width:55%; text-align: left;"><?php echo $asDetailUnite['Nom_unite'] ?></td>
					</tr>
			</table>
			<?php
				if(empty($aListesAgents))
				{
					echo '<hr style=" margin-right:17px; width:818px;"/>';
				}
				$idAgentPrec = 0;
				
				foreach ($aListesAgents as $iAg => $aRow)
				{
					$iNbTableHtml++;
					$sSpaceAgent = ''; $sSpacePDF = '';
					if($iAg>0) 
					{
						$sSpacePDF = "<p>&nbsp;</p>";
						$sSpaceAgent = "<tr><td>&nbsp;</td></tr>";
					}
					$idAgentPrec = intval($aAgent['Identifiant_agent']);
					echo $sSpacePDF;
			?>
				<table style="width:400px; text-align: left">
			<?php 
					echo $sSpaceAgent;
					if ($sEdition != '134' && $sEdition != '136')
					{ 
			?>
					<tr>
						<td style="width:20%; text-align: left;">Agent : </td>
						<td style="width:25%; text-align: left;"><?php echo $aRow['Code_agent'] ?></td>
						<td style="width:55%; text-align: left;"><?php echo $aRow['Nom_agent'] ?></td>
					</tr>
			<?php 
					} 
					$aListes = array();
					$bShowEntDetail = true;
					switch ($sEdition)
					{
						case '135':
								$aListes = $oEditions->listeActesDeGestionAgent($aRow['Identifiant_agent'],$sDateDebut,$sDateFin, $aRow['Identifiant_unite']);
								if(empty($aListes)) $bShowEntDetail = false;
								break;
						
						case '137':
								$aListes = $oEditions->listeSinistresDeclaresAgent($aRow['Identifiant_agent'],$sDateDebut,$sDateFin);
								break;

						case '138':
								$aListes = $oEditions->listeSinistresReglesAgent($aRow['Identifiant_agent'],$sDateDebut,$sDateFin);
								if(empty($aListes)) $bShowEntDetail = false;
								break;

						case '139':				
								$aListes = $oEditions->listeSouscriptionsAgent($aRow['Identifiant_agent'],$sDateDebut,$sDateFin);
								break;

					}
			?>
				</table>
				<hr style=" margin-right:17px; width:818px;"/>
				<table class="tab_list" style="border:1px solid black; border-collapse:collapse;" border="1" >
			<?php 
				//debut: on n'affiche pas l'entete du tableau detail si l'agent n'a pas d'acte de gestion
				if( $bShowEntDetail )
				{
			?>
						<tr id="niv1">
						<?php
						$i = 1;					
						
						foreach ($aEnteteTab as $iKey => $aEntete)
						{
							$sClass = "col".$i;
							$sClassOther = '';
							if($i==7)  $sClass = "col6"; 
							if($aEntete['nom_colonne']=='Type') $sClass = "col14"; 
							if($aEntete['nom_colonne']== 'Date effet') $sClass = "col1";
							if(($sEdition == '138')) 
							{
								
								if($aEntete['nom_colonne']== 'Date effet')
									$sClassOther = "nowrap='nowrap'";
								if($aEntete['nom_colonne']== 'Date r&eacute;glement')
									$sClass = "col15";
								if($aEntete['nom_colonne']== 'Offre')
									$sClass = "col15";
							}
							if($aEntete['nom_colonne']== 'Cotisation') $sClass = "col2";
							if($aEntete['nom_colonne']== "Droit entr&eacute;e") $sClass = "col3";
							if($aEntete['nom_colonne']== "Fr&eacute;quence") $sClass = "col3";
					?>
							<td class='<?php echo $sClass ?>' <?php echo $sClassOther ?> style='text-align:center; height:25px; width:<?php echo ${'aPdfWidthRecord_'.$sEdition}[$aEntete['nom_colonne']]?>%; font-weight:bold;  ' ><?php echo $aEntete['nom_colonne']?></td>
					<?php
							$i++;
						} 
					?>
						</tr>

					<?php
				}//fin: on n'affiche pas l'entete du tableau detail si l'agent n'a pas d'acte de gestion	
				
					foreach ($aListes as $iKey => $aElement)
					{
							?>
				
						<tr id='niv2'>
							<td  style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Contrat']?>%; text-align:center;" ><?php echo $aElement['Numero_contrat']?></td>
							<td  align='center'  style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Date effet']?>%; " ><?php echo formaterDate($aElement['Date_effet_contrat'])?></td>
							<td  align='center'  style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Offre']?>%;" ><?php echo htmlentities($aElement['Libelle_offre'], ENT_COMPAT,'utf-8');?></td>
							<td  align='center'  style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Produit']?>%; " ><?php echo htmlentities($aElement['Libelle_produit'], ENT_COMPAT,'utf-8');?></td>
							<td  align='center'  style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Formule']?>%; " ><?php echo htmlentities($aElement['Libelle_formule'], ENT_COMPAT,'utf-8');?></td>
				
				
							<?php if (isset($aElement['Montant_base_cotisation'])){
								// SOUSCRIPTIONS
								$fTotalCotisation += $aElement['Montant_base_cotisation']+$aElement['Montant_frais_echeance_cotisation'];
								$iNbContrat++;
								$fTotalDE += $aElement['Montant_droits_entree_cotisation'];
								
								//pour l'unite
								$iNbContratUnite++; 
								$fTotalCotisationUnite += $aElement['Montant_base_cotisation']+$aElement['Montant_frais_echeance_cotisation'];
								$fTotalDEUnite += $aElement['Montant_droits_entree_cotisation'];
								
								?>
							<td  align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Cotisation']?>%;"><?php echo $aElement['Montant_base_cotisation']+$aElement['Montant_frais_echeance_cotisation']?></td>
							<td  align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Droit entr&eacute;e']?>%; "><?php echo $aElement['Montant_droits_entree_cotisation']?></td>
							<?php } ?>
				
							<?php if (isset($aElement['Nom_element'])){
								// Actes de gestion
								if (!isset($aActe[$aElement['Nom_element']]))
								{
									$aActe[$aElement['Nom_element']] = 1;
									
									if (!isset($aActeUnite[$aElement['Nom_element']]))
										$aActeUnite[$aElement['Nom_element']] = 1;
									else $aActeUnite[$aElement['Nom_element']]++;
								}
								else
								{
									$aActe[$aElement['Nom_element']]++;
									$aActeUnite[$aElement['Nom_element']]++;
								}
								$iTotalGeneral++;
								?>
							<td style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Acte de gestion']?>%; text-align:center;" ><?php echo htmlentities($aElement['Nom_element'], ENT_COMPAT,'utf-8');?></td>
							<?php } ?>
				
							
				
							<?php if (isset($aElement['Frequence_paiement'])){ ?>
							<td align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Fr&eacute;quence']?>%; "><?php echo $oElementDico->elementdico_getNomFromCode($aElement['Frequence_paiement'],'M0007') ?></td>
							<?php } ?>
				
							
				
							<?php if (isset($aElement['Type_sinistre'])){
								// SINISTRES
								?>
							<td align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Type']?>%; "><?php echo $oElementDico->elementdico_getNomFromCode($aElement['Type_sinistre'], dataObject_dictionnaire::TYPE_SINISTRE)?></td>
							<td align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Date <br/>survenance&nbsp;déclaration']?>%; "><?php echo formaterDate($aElement['Date_survenance_sinistre'])." &nbsp; ".formaterDate($aElement['Date_declaration_sinistre'])?></td>
							<?php } ?>
				
							<?php if (isset($aElement['Type_prestation'])){
								// SINISTRES REGLES
								if (!isset($aSinistreReg[$aElement['Type_sinistre']]))
								{
									$aSinistreReg[$aElement['Type_sinistre']] = 1;
									$aSinistreRegUnite[$aElement['Type_sinistre']] = 1;
								}
								else
								{
									$aSinistreReg[$aElement['Type_sinistre']]++;
									$aSinistreRegUnite[$aElement['Type_sinistre']]++;
								}
								
								if (!isset($aPrestation[$aElement['Type_sinistre']][$aElement['Type_prestation']]))
								{
									$aPrestation[$aElement['Type_sinistre']][$aElement['Type_prestation']] = $aElement['Montant_prestation'];
									$aPrestationUnite[$aElement['Type_sinistre']][$aElement['Type_prestation']] = $aElement['Montant_prestation'];
								}
								else
								{
									$aPrestation[$aElement['Type_sinistre']][$aElement['Type_prestation']] += $aElement['Montant_prestation'];
									$aPrestationUnite[$aElement['Type_sinistre']][$aElement['Type_prestation']] += $aElement['Montant_prestation'];
								}
								$iTotalGeneral++;
								
								$fTotalPrestation += $aElement['Montant_prestation'];
								?>
							<td align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Type prestation']?>%; "><?php echo $oElementDico->elementdico_getNomFromCode('N'.$aElement['Type_prestation'], dataObject_dictionnaire::TYPE_PRESTATION)?></td>
							<td align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Montant']?>%; "><?php echo $aElement['Montant_prestation']?></td>
							<td align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Date r&eacute;glement']?>%;"><?php echo formaterDate($aElement['Date_evenement'])?></td>
							<?php } ?>
				
							<?php if (isset($aElement['Etat_sinistre'])){
								// SINISTRES DECLARES
								if (!isset($aSinistreDec[$aElement['Type_sinistre']]))
								{
									$aSinistreDec[$aElement['Type_sinistre']] = 1;
									$aSinistreDecUnite[$aElement['Type_sinistre']] = 1;
								}
								else
								{
									$aSinistreDec[$aElement['Type_sinistre']]++;
									$aSinistreDecUnite[$aElement['Type_sinistre']]++;
								}
								$iTotalGeneral++;
								?>
							<td align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Etat']?>%; "><?php echo $oElementDico->elementdico_getNomFromCode($aElement['Etat_sinistre'], dataObject_dictionnaire::_ETAT_SINISTRE)?></td>
							<td align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['D&eacute;cision']?>%; "><?php echo $oElementDico->elementdico_getNomFromCode($aElement['Decision_partenaire_sinistre'], dataObject_dictionnaire::_TYPE_DECISION_ASSUREUR)?></td>
							<td align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Motif refus']?>%; "><?php echo $oElementDico->elementdico_getNomFromCode($aElement['Motif_refus_sinistre'], dataObject_dictionnaire::MOTIF_REFUS_INDEMNISATION)?></td>
							<?php } ?>
						</tr>
						<?php 
					} //fin parcours element
					
					if (isset($aActe))
					{
						$sTotalAgent = '';
							foreach ($aActe as $iKey => $sVal)
							{
								$sTotalAgent .= "
								<tr id='niv2'>
									<td  class='col1' style=' width:30%; '>Agent ".$aRow['Nom_agent']."</td>
									<td  class='col7' style=' width:40%; ' align='center'>".$iKey."</td>
									<td  class='col7' style=' width:30%; ' align='center'>".$sVal."</td>
								</tr>";
							}
							if($sTotalAgent =='')
								$sTotalAgent = "
								<tr id='niv2'>
									<td class='col1' style=' width:30%;'>Agent ".$aRow['Nom_agent']."</td>
									<td class='col7' align='center' style=' width:40%; '>-</td>
									<td class='col7' align='center' style=' width:30%; '>0</td>
								</tr>";
							$aActe = array();
						echo $sSepareTable.$sTabActe;
					}
					else if (isset($aSinistreDec))
					{
						$sTotalAgent = '';
						foreach ($aSinistreDec as $iKey => $sVal)
						{
								$sTotalAgent .= "
									<tr id='niv2'>
										<td  class='col1' style=' width:34%; ' >Agent ".$aRow['Nom_agent']."</td>
										<td class='col7' align='center'  style=' width:33%; '>".$oElementDico->elementdico_getNomFromCode($iKey, dataObject_dictionnaire::TYPE_SINISTRE)."</td>
										<td class='col7' align='center'  style=' width:33%; '>".$sVal."</td>
									</tr>";
						}
						$aSinistreDec = array();
					
						echo $sSepareTable.$sTabSinistreDec;
					}
					else if (isset($aSinistreReg))
					{
						$sTotalAgent = '';
						foreach ($aSinistreReg as $iKey => $sVal)
						{
								foreach ($aPrestation[$iKey] as $iKey1 => $sVal1)
								{
									$sTotalAgent .= "
										<tr id='niv2'>
											<td class='col1' style=' width:22%;'>Agent ".$aRow['Nom_agent']."</td>
											<td class='col3' align='center' style=' width:20%;'>".$oElementDico->elementdico_getNomFromCode($iKey, dataObject_dictionnaire::TYPE_SINISTRE)."</td>
											<td class='col1' align='center' style=' width:18%;'>".$sVal."</td>
											<td class='col3' align='center' style=' width:20%;'>".$oElementDico->elementdico_getNomFromCode('N'.$iKey1, dataObject_dictionnaire::TYPE_PRESTATION)."</td>
											<td class='col1' align='center' style=' width:20%;'>".$sVal1."</td>
										</tr>";
									$iMontPrest += $sVal1;
								}
							}
						$aSinistreReg = array();
						if($sTotalAgent =='')
								$sTotalAgent = "
										<tr id='niv2'>
											<td class='col1' style=' width:22%; '>Agent ".$aRow['Nom_agent']."</td>
											<td class='col3' align='center' style=' width:20%; '>-</td>
											<td class='col1' align='center' style=' width:18%; '>0</td>
											<td class='col3' align='center' style=' width:20%; '>-</td>
											<td class='col1' align='center' style=' width:20%; '>0</td>
										</tr>";
						echo $sSepareTable.$sTabSinistreReg;
					}
					else
					{
							$sTotalAgent = "
							<tr id='niv2'>
								<td class='col1' style=' width:24%;'>Agent ".$aRow['Nom_agent']."</td>
								<td class='col3' align='center' style=' width:26%;'>".$iNbContrat."</td>
								<td class='col2' align='center' style=' width:30%;'>".$fTotalCotisation."</td>
								<td class='col9' align='center' style=' width:20%;'>".$fTotalDE."</td>
							</tr>";
							$fCotisation += $fTotalCotisation;
							$fDroit += $fTotalDE;
							$fTotalCotisation = $iNbContrat = $fTotalDE = 0;
							
							echo $sSepareTable.$sTabSous;
					}
					echo $sTotalAgent;
				}
				
				if (isset($aActeUnite))
				{
					$sTotalUnite = '';
						foreach ($aActeUnite as $iKey => $sVal)
						{
							$sTotalUnite .= "
							<tr id='niv2'>
								<td class='col1' style=' width:24%; '>Unit&eacute; ".$aListesAgents[0]['Nom_unite']."</td>
								<td class='col7' align='center' style=' width:24%; '>".$iKey."</td>
								<td class='col7' align='center' style=' width:28%; '>".$sVal."</td>
							</tr>";
						}
					if($sTotalUnite=='')
						$sTotalUnite = "
							<tr id='niv2'>
								<td class='col1' style=' width:24%; '>Unit&eacute; ".$asDetailUnite['Nom_unite']."</td>
								<td class='col7' align='center' style=' width:24%; '>-</td>
								<td class='col7' align='center' style=' width:28%; '>0</td>
							</tr>";
					echo $sSepareTable.$sTabActe;
				}
				else if (isset($aSinistreDecUnite))
				{
					$sTotalUnite = '';
						foreach ($aSinistreDecUnite as $iKey => $sVal)
						{
							$sTotalUnite .= "
							<tr id='niv2'>
							<td  class='col1' style=' width:34%; '>Unit&eacute; ".$aListesAgents[0]['Nom_unite']."</td>
							<td class='col7' align='center'  style=' width:33%; '>".$oElementDico->elementdico_getNomFromCode($iKey, dataObject_dictionnaire::TYPE_SINISTRE)."</td>
							<td class='col7' align='center'  style=' width:33%; '>".$sVal."</td>
							</tr>";
						}
					if($sTotalUnite == '')
								$sTotalUnite .= "
										<tr id='niv2'>
											<td  class='col1' style=' width:34%;'>Unit&eacute; ".$asDetailUnite['Nom_unite']."</td>
											<td class='col7' align='center' style=' width:33%;'>-</td>
											<td class='col7' align='center' style=' width:33%;'>0</td>
										</tr>";
					if(empty($aListesAgents)) echo $sSepareTableVide.$sTabSinistreDec;
					else echo $sSepareTable.$sTabSinistreDec;
					
				}
				else if (isset($aSinistreRegUnite))
				{
						foreach ($aSinistreRegUnite as $iKey => $sVal)
						{
							foreach ($aPrestationUnite[$iKey] as $iKey1 => $sVal1)
							{
								$sTotalUnite .= "
								<tr id='niv2'>
								<td class='col1'>Unit&eacute; ".$aListesAgents[0]['Nom_unite']."</td>
								<td class='col3' align='center'>".$oElementDico->elementdico_getNomFromCode($iKey, dataObject_dictionnaire::TYPE_SINISTRE)."</td>
								<td class='col1' align='center'>".$sVal."</td>
								<td class='col3' align='center'>".$oElementDico->elementdico_getNomFromCode('N'.$iKey1, dataObject_dictionnaire::TYPE_PRESTATION)."</td>
								<td class='col1' align='center'>".$sVal1."</td>
								</tr>";
								$iMontPrest += $sVal1;
							}
						}
						if($sTotalUnite == '')
							$sTotalUnite = "
								<tr id='niv2'>
								<td class='col1'>Unit&eacute; ".$asDetailUnite['Nom_unite']."</td>
								<td class='col3' align='center'>-</td>
								<td class='col1' align='center'>0</td>
								<td class='col3' align='center'>-</td>
								<td class='col1' align='center'>0</td>
								</tr>";
						echo $sSepareTable.$sTabSinistreReg;
				}
				else
				{
						$sTotalUnite = "
					<tr id='niv2'>
						<td class='col1' style=' width:24%;'>Unit&eacute; ".$asDetailUnite['Nom_unite']."</td>
						<td class='col3' align='center' style=' width:26%;'>".$iNbContratUnite."</td>
						<td class='col7' align='center' style=' width:30%;'>".$fTotalCotisationUnite."</td>
						<td class='col7' align='center' style=' width:20%;'>".$fTotalDEUnite."</td>
					</tr>";
						echo $sSepareTable.$sTabSous;
				}
			
			echo $sTotalUnite;
			if(empty($aListesAgents)) echo '</table>';
				
			for($i=1;$i<=count($aListesAgents);$i++) 
			{
			?>
				</table>
			<?php
			}	
			?>
			</div> <!-- fin content_table -->
				
<?php
	}
?>
		
				</PDF>	    
			</div>
			
			<div class="imgright1"></div>			
			<div class="cleared"></div> 
			<?php

	$sHtml = ob_get_contents();
	//$sNomListe = utf8_decode($sNomListe);
	$sNomfichier = preg_replace('/&eacute;/','e',$sNomListe);
	$sNomfichier = preg_replace('/&egrave;/','e',$sNomfichier);
	$sNomfichier = preg_replace('/&#130;/','_',$sNomfichier);
	$sNomfichier = str_replace("d'",'',$sNomfichier);
	$sNomfichier = str_replace("é",'e',$sNomfichier);
	$oFichierPDF = new CHMTL_PDF($sHtml, $sNomfichier,'L',false, false);
	$sUrlPrint = "succes_impression.php?file=".$oFichierPDF->sFileEdition;
	?>
			<form action="<?php echo $sEditFormAction ?>" method="post" name="form1" id="form1" class="formulaire">
				<input type="hidden" name="nomListe" maxlength="50" class="resizeall" value="<?php echo $sNomListe ?>" size="32" />
				<table>
					<tr>
						<td class='col15'>&nbsp;&nbsp;ADRESSE MAIL</td>
						<td ><input type="text" name="OEM_email" maxlength="50" class="resizeall" value="" size="32" /></td>
					</tr>
				</table>
			
				<input type="hidden" name="file" value="<?php echo $oFichierPDF->sEdition; ?>" />
				<input type="hidden" name="deb" value="<?php echo formaterDate($sDateDebut,"-","/",false); ?>" />
				<input type="hidden" name="fin" value="<?php echo formaterDate($sDateFin,"-","/",false); ?>" />
				
				<div class="bt1">
					<span class="btleft"></span> <a href="../editions"> 
					<input type="submit" name="Retour" value="Retour" class="resizeall btmiddle" />
					<span class="btright"></span> </a>
				</div>
				<div class="bt1">
					<span class="btleft"></span> 
					<input type="submit" name="Envoyer" value="Envoyer par mail" class="resizeall btmiddle" /> 
					<span class="btright"></span>
				</div>
			</form>
			<div class="bt1">
				<span class="btleft"></span> 
				<input type="submit" name="Imprimer" value="Imprimer" class="resizeall btmiddle" onclick="javascript:printEdition('<?php echo $sUrlPrint ?>');" />
				<span class="btright"></span>
			</div>
			
			<!--/***************************************/-->
		</div>
	</body>
</html>
	
		<?php
		include_once PATH_INCLUDE.'inc_pied_page.php';

/**
 * fonction qui permet de changer le format d'une date 
 * 
 * @param  string $sDate date à formater : au format "JJ-MM-AAAA", "JJ/MM/AAAA", "AAAA-MM-JJ" ou "AAAA/MM/JJ"
 * @param  char $sSeparateur sépareteur de la date à formater : vaut "-" ou "/"
 * @param  char $NouveauSeparateur sépareteur de la date formater (resultat) : vaut "-" ou "/"
 * @param  bool $bInverser : vaut true si on veut inveser le format de la date etré en parametre et false sinon
 * @return string : au format "JJ-MM-AAAA", "JJ/MM/AAAA", "AAAA-MM-JJ" ou "AAAA/MM/JJ"
 */
function formaterDate($sDate, $sSeparateur = "-", $NouveauSeparateur = "-", $bInverser = true)
{
	$aTab = explode($sSeparateur, $sDate);
	
	if ($bInverser)
		$sRetour = $aTab[2].$NouveauSeparateur.$aTab[1].$NouveauSeparateur.$aTab[0];
	else
		$sRetour = $aTab[0].$NouveauSeparateur.$aTab[1].$NouveauSeparateur.$aTab[2];
	
	return $sRetour;
}
		?>